package com.cumt.threeway.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cumt.threeway.activity.MainManageActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class BannerChangeAdapter extends PagerAdapter {
	
	private List<ImageView> mImageViewList;
	private ImageLoader mImageLoader = ImageLoader.getInstance();
	private Context mContext;
	
	
	public BannerChangeAdapter(Context context, ImageLoader imageLoader, List<ImageView> list) {
		mImageLoader = imageLoader;
		mImageViewList = list;
		mContext = context;
	}
	
	@Override
	public int getCount() {
		return mImageViewList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		ImageView imageView = mImageViewList.get(position);
		ViewPager viewPager = (ViewPager) container;
		viewPager.removeView(imageView);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = mImageViewList.get(position);
		imageView.setOnClickListener(new MyClickListener(position));
		mImageLoader.displayImage(imageView.getTag() + "", imageView);
		ViewPager viewPager = (ViewPager) container;
		viewPager.addView(imageView);
		return imageView;
	}
	
	private class MyClickListener implements OnClickListener{
		
		private int mPosition = 0;
		
		private MyClickListener(int position) {
			mPosition = position;
		}

		@Override
		public void onClick(View arg0) {
			switch (mPosition) {
				case 0:
					((MainManageActivity)mContext).goOtherActivity(0);
					break;
				case 1:
					((MainManageActivity)mContext).goOtherActivity(1);
					break;
				case 2:
					((MainManageActivity)mContext).goOtherActivity(2);
					break;
				case 3:
					((MainManageActivity)mContext).goOtherActivity(3);
					break;
				default:
					break;
			}
		}
		
	}
}
